
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/basic_form_style.css" rel="stylesheet" type="text/css" />

<!--start autocomplete support-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script src="jquery/dudi/jquery.autocomplete.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/autocomplete_styles.css" type="text/css" media="screen" />
<script type="text/javascript">
 //<![CDATA[
 var a1;
 jQuery(function() {
 var options = {
 serviceUrl: 'content/autocomplete_room.php',
 width: 300,
 delimiter: /(,|;)\s*/,
 deferRequestBy: 0, //miliseconds
 params: { country: 'Yes' },
 noCache: false //set to true, to disable caching
 };
 a1 = $('#query').autocomplete(options);

 $('#navigation a').each(function() {
 $(this).click(function(e) {
 var element = $(this).attr('href');
 $('html').animate({ scrollTop: $(element).offset().top }, 300, null, function() { document.location = element; });
 e.preventDefault();
 });
 });

 });

//]]>
</script>
<style type="text/css">
	.autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:6px 0 0 6px; /* IE6 fix: */ _background:none; _margin:1px 0 0 0; }
	.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px;  _margin:0; _overflow-x:hidden; }
	.autocomplete .selected { background:#F0F0F0; }
	.autocomplete div { padding:2px 5px; white-space:nowrap; overflow:hidden; }
	.autocomplete strong { font-weight:normal; color:#3399FF; }
</style>
<!--end autocomplete support-->

</head>
<script type="text/javascript">
//changing the hiiden input to newvalue (addmember/removemembers)
function changeActionType(newvalue){
	document.forms.roommanagement.actiontype.value = newvalue;		
	document.forms.roommanagement.submit();
}
</script>
<?php 
//Rquire MySqlAdapter.
require_once(dirname(__FILE__).'/../db/MySqlAdapter.php');

//echo "<br>Step 1: open a connection to the DB<br>";
$conn = MySqlAdapter::connect1();

//select Schema of DataBase	
if ($conn):
	$result = MySqlAdapter::connect2();
else:
	die ("Could not connect to db " . mysql_error());
endif;

//echo "<body>";
//if button was clicked in the 'roommanagement' form, do the action:
if(isset($_POST['actiontype']) && (isset($_POST['roommembers']) || isset($_POST['roomnumber']))){
	//what button was clicked:
	switch ($_POST['actiontype']) {
		case "0":
			//echo "Error: actiontype equals 0";
			break;
		case "removemember":
			//for each room member that was selected: remove him from the room. his new room will be number zero.
			foreach ($_POST['roommembers'] as $rm )
			{					
				$query = "UPDATE `ohel_shelomo_2012`.`students` SET `room` = '0' WHERE `students`.`id` =".$rm." ;";
				$result = mysql_query($query) or die ("Invalid update: " . mysql_error());		
			}
			break;
		case "addmember":
			//add user with member id to the list of room member. updating DataBase
			$query = "UPDATE `ohel_shelomo_2012`.`students` SET `room` = '".$_POST['roomnumber']."' WHERE `students`.`id` =".$_POST['addid']." ;";
			$result = mysql_query($query) or die ("Invalid update: " . mysql_error());	
			break;
		default:
			break;
	}
}else
	//echo "Nothing is isset";
?>

<body>
<center>
<h1  class="primeHead">Room Management</h1>
<table width="54%" border="1" cellpadding="0" bordercolor=#FFFFFF>
  <tr>
    <th width="19%" scope="row" class="subHead">select room number:</th>
    <td width="81%">
<form action="index.php?s=9" method="post" name="selectroom" target="_self">
<input name="roomnumber" id="query" type="text" size="15" maxlength="5" /> <input name="veiw" type="submit" value="veiw" />
</form>
	</td>
  </tr>
  <tr>
    <th scope="row" class="subHead">veiw room details:</th>
    <td>

<?php
//getting data from DB to the form: "room-management" after search room was clicked
if( isset($_POST['roomnumber']) && $_POST['roomnumber']!="" ){
		//select Schema of DataBase	
		if ($conn):
			$result =  MySqlAdapter::connect2();
		else:
			die ("Could not connect to db " . mysql_error());
		endif;	
		
		//select all room members of the selected room:
		$query = "SELECT  person.first_name, person.last_name, person.id FROM person LEFT JOIN students ON students.id = person.id  WHERE students.room=".$_POST['roomnumber'].";";
		$result = mysql_query($query) or die ("Invalid insert " . mysql_error());		
?>

<form action="index.php?s=9" method="post" name="roommanagement" target="_self">





<table width="99%" border="1" cellpadding="0">
  <tr>
    <th width="27%" scope="row">Room Number:</th>
    <td width="73%"><input name="roomnumber" type="text" value = <?php echo $_POST['roomnumber'];?> size="5" maxlength="5" readonly /></td>
  </tr>
  <tr>
    <th scope="row">Members:</th>
    <td>
<table width="100%" border="1" cellpadding="0">
  <tr>
  	<input type="hidden" name="actiontype" value="0">
    <th width="70%" scope="row"><select name="roommembers[]" multiple="multiple" size="10" width="100" style="width: 100px">
<?php 


	while ($row = mysql_fetch_array($result))
	{
		echo "<option value='".$row['id']."'>".$row['first_name']." ".$row['last_name']."</option>";
	}				
?>

    </select><br /><input name="removememberbtn" type="button" onClick="changeActionType('removemember');" value="remove-member" /></th>
    <td width="30%">Add Member:<br />ID: <input name="addid" type="text" size="10" maxlength="9" /><br /><input name="add" type="button" value="Add" onClick="changeActionType('addmember');"/></td>
  </tr>
</table>    
    </td>
  </tr>
  <tr>
    <th scope="row">Room Equipment:</th>
    <td><select name="equipment-list" size="15" width="350" style="width: 350px" multiple>
      
<?php
	//choose all equipment that was boorowed and didn't returned back from this roomnumber:
	$query = "SELECT `equipment`.`name` , `person`.`first_name` , `person`.`last_name`
FROM `equipment`
LEFT JOIN `reservation` ON `equipment`.`id` = `reservation`.`equipmentID`
LEFT JOIN `person` ON `reservation`.`userID` = `person`.`id`
LEFT JOIN `students` ON `person`.`id` = `students`.`id`
WHERE `reservation`.`wastaken` = '1'
AND `wasreturned` = '0'
AND `students`.`room` =".$_POST['roomnumber'].";";
	$result = mysql_query($query) or die ("Invalid update: " . mysql_error());
				 
	while ($row = mysql_fetch_array($result))
	{
		echo "<option>".$row['name']." BY ".$row['first_name']." ".$row['last_name']."</option>";
	}				
?>
      
      
      nbsp</select><br /><br />
      <!--<input name="cancel-room-management" type="button" value="cancel" /><input name="submit-room-managment" type="submit" value="save" />--></td>
  </tr>
</table>
</form> 

<?php
}
?>

       
    </td>
  </tr>
</table>
<p>&nbsp;</p>
<p><img src="image/room-management.jpg" width="599" height="371" alt="room-management" /></p>
</center>

</body>
<?php
mysql_close($conn);
?>
</html>
